/*
 * SvrInfo.h
 *
 *  Created on: Apr 3, 2013
 *      Author: root
 */

#ifndef SVRINFO_H_
#define SVRINFO_H_

#include "utils.h"
#include "SvrConfig.h"
/*
 * server cpu and memory status info
 */
struct MEM_OCCUPY
{
	 char name[20];
	 unsigned long total;
	 char name2[20];
	 unsigned long free;
	 char name3[20];
	 unsigned long buffers;
	 char name4[20];
	 unsigned long cached;
	 char name5[20];
	 unsigned long swapcached;
};
struct CPU_OCCUPY
{
	 char name[20];      //定义一个char类型的数组名name有20个元素
	 unsigned int user; //定义一个无符号的int类型的user
	 unsigned int nice; //定义一个无符号的int类型的nice
	 unsigned int system;//定义一个无符号的int类型的system
	 unsigned int idle; //定义一个无符号的int类型的idle
	 unsigned int iowait;
	 unsigned int irq;
	 unsigned int softirq;
};
struct NetIOStat
{
	//bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
	char name[20];
	unsigned long long recvbytes;
	unsigned long long recvpackets;
	unsigned long long recverrs;
	unsigned long long recvdrop;
	unsigned long long recvfifo;
	unsigned long long recvframe;
	unsigned long long recvcompressed;
	unsigned long long recvmulticast;

	unsigned long long transbytes;
	unsigned long long transpackets;
	unsigned long long transerrs;
	unsigned long long transdrop;
	unsigned long long transfifo;
	unsigned long long transcolls;
	unsigned long long transcarrier;
	unsigned long long transcompressed;

};

/*
 * SvrInfo class
 * */
class SvrInfo {
public:
	SvrInfo();
	virtual ~SvrInfo();

	/*
	 * inti SvrInfo class
	 **/
	void init(SvrConfig * pconfig,const char *corver);

	/*
	 * refresh svr info
	 * uticks,useconds
	 */
	void refresh(long useconds,int connum,int confree,int conpoolsize,int iofreenum,int iototal,int tokennum,int tokentotal,int snum ,int sfree,
				int streamcount,int freestreamcount,
				int spkt,int fspkt,
				int dchunkcount,int freedchunkcount,
				int dstreamcount,int freedstramcount);


	/**
	 *read SvrInfo
	 */
	const char * readSvrInfo();


	/**
	* get server status info
	*/
	void get_memoccupy (MEM_OCCUPY *mem);
	void get_netoccupy (NetIOStat * nstat);
	void get_cpuoccupy (CPU_OCCUPY *cpust);
	int  cal_cpuoccupy (CPU_OCCUPY *o, CPU_OCCUPY *n);

private:
	SvrConfig  *   pSvrconfig;
	time_t         start_time;

	char netcorver[64];
	char infodata[4096];

	int connnum;
	int connfree;
	int connpoolsize;

	int iofreebufnum;
	int iobuftotalnum;

	int conntokennum;
	int tokentotalnum;

	int sessionnum;
	int freesessionnum;

	int _streamcount;
	int _freestreamcount;
	int _spkt;
	int _fspkt;
	int _dchunkcount;
	int _freedchunkcount;
	int _dstreamcount;
	int _freedstramcount;

	CPU_OCCUPY     cpu_stat1;
	CPU_OCCUPY     cpu_stat2;
	int 		   cpu_use;

	unsigned long long totalBlocks;
	unsigned long long totalSize;
	unsigned long long freeDisk;

	unsigned long  mem_free;
	unsigned long  mem_total;

	char nstat_name[20];
	unsigned long long nstat_recvbytes;
	unsigned long long nstat_recvpackets;
	unsigned long long nstat_transbytes;
	unsigned long long nstat_transpackets;

	int 		   CpuUseTick;
};

#endif /* SVRINFO_H_ */
